function User() {

	var ids = {
		edit : "#user-edit-0",
		save : "#user-edit-save",
		back : "#user-edit-back",
		errorList : "#user-list-error",
		errorEdit : "#user-edit-error",
		contentList : "#content-user-list",
		contentEdit : "#content-user-edit",
		formEdit : "#user-edit-form",
		listBody : "#user-list-body",
		pagination : "#user-list-pagination",
		paginationFrom : "#user-list-pagination-from",
		paginationTo : "#user-list-pagination-to",
		paginationFirst : "#user-list-pagination-first",
		paginationPrevious : "#user-list-pagination-previous",
		paginationNext : "#user-list-pagination-next",
		paginationLast : "#user-list-pagination-last"
	};

	var urls = {
		list : contextPath + "/users?paging=true",
		edit : contextPath + "/user/",
		remove : contextPath + "/user/",
		save : contextPath + "/user",
		store : {
			list : contextPath + "/stores"
		}
	};

	var pagination = {
		page : 0
	};

	this.init = function() {
		$(ids.edit).click(function(event) {
			event.preventDefault();

			edit(0);
		});
		$(ids.save).click(function(event) {
			event.preventDefault();

			save();
		});
		$(ids.back).click(function(event) {
			event.preventDefault();

			back();
		});

		list();
	};

	var list = function() {
		$(ids.errorList).hide();

		var url = (urls.list + "&start=" + pagination.page + "&offset=" + properties.pagination.offset);

		$.ajax({
			type : "GET",
			url : url,
			dataType : "json",
			success : success.list
		});
	};

	var remove = function(id) {
		$(ids.errorList).hide();

		$("#loading").show();
		$(ids.contentList).hide();

		var url = (urls.remove + id);

		$.ajax({
			type : "DELETE",
			url : url,
			dataType : "json",
			success : success.remove
		});
	};

	var edit = function(id) {
		$("#loading").show();
		$(ids.contentList).hide();

		var requests = new Array();

		if (id > 0) {
			requests.push(urls.edit + id);
			// requests[index] = function() {
			// return $.ajax({
			// type : "GET",
			// url : url,
			// dataType : "json"
			// // ,success : success.edit
			// });
			// };
			// } else {
			// success.edit({
			// entity : {
			// id : 0
			// }
			// });
		}

		requests.push(urls.store.list);

		// requests[index] = function() {
		// return $.ajax({
		// type : "GET",
		// url : urls.store.list,
		// dataType : "json"
		// ,success : function(data, textStatus, jqXHR) {
		// stores = data.entities;
		// }
		// });
		// };

		// var def = $.Deferred();
		//
		// $.each(requests, function(index, request) {
		// def.pipe($.ajax({
		// type : "GET",
		// url : request
		// }).done(function(data) {
		// console.debug(data);
		// }));
		// });
		//
		// def.done(function(data) {
		// console.debug(data);
		// });
		//

		// var test = $.map(requests, function(request, index) {
		// console.debug(request);
		// console.debug(index);
		// }).toArray();
		//
		// console.debug(test);

		$.when($.map(requests, function(request, index) {
			return $.ajax({
				type : "GET",
				url : request,
				async : true
			});
		})).done(function(result1, result2) {
			console.debug(result1);

			for ( var index = 0; index < result1.length; index++) {
				console.debug(result1[index]);
			}

			console.debug(result2);

			for ( var index = 0; index < result2.length; index++) {
				console.debug(result2[index]);
			}
		});

		// $.when($.ajax({
		// type : "GET",
		// url : (urls.edit + id)
		// }), $.ajax({
		// type : "GET",
		// url : urls.store.list
		// })).done(function(arguments1, arguments2) {
		// console.debug("1");
		//
		// for ( var index = 0; index < arguments1.length; index++) {
		// console.debug("index: " + index);
		// console.debug(arguments1[index]);
		// }
		//
		// console.debug("2");
		//
		// for ( var index = 0; index < arguments2.length; index++) {
		// console.debug("index: " + index);
		// console.debug(arguments2[index]);
		// }
		// });

		console.debug("done!");

		$("#loading").hide();
		$(ids.contentList).show();

		// $.when(requests[0], requests[1]).then(function() {
		// alert("uia");
		// });
	};

	var save = function() {
		$("#loading").show();
		$(ids.contentEdit).hide();

		var url = (urls.save);

		$.ajax({
			type : "POST",
			url : url,
			dataType : "json",
			data : $(ids.formEdit).serialize(),
			success : success.save
		});
	};

	var back = function() {
		$(ids.contentEdit).hide();
		$(ids.contentList).show();
	};

	var success = {};

	success.list = function(data, textStatus, jqXHR) {
		var tbodyElement, trElement, tdElement, aElement, imgElement;

		tbodyElement = $(ids.listBody);
		tbodyElement.empty();

		var entity;

		for ( var index = 0; index < data.entities.length; index++) {
			entity = data.entities[index];

			trElement = $(document.createElement("tr"));

			if ((index % 2) == 1) {
				trElement.attr("class", "odd");
			}

			tdElement = $(document.createElement("td"));
			tdElement.text(entity.userName);

			trElement.append(tdElement);

			imgElement = $(document.createElement("img"));
			imgElement.attr("class", "image");
			imgElement
					.attr("src", contextPath + "/resources/images/update.png");

			aElement = $(document.createElement("a"));
			aElement.append(imgElement);
			aElement.attr("href", "#");
			aElement.attr("alt", labels.entity.edit);
			aElement.attr("title", labels.entity.edit);
			aElement.click({
				id : entity.id
			}, function(event) {
				event.preventDefault();

				edit(event.data.id);
			});

			tdElement = $(document.createElement("td"));
			tdElement.append(aElement);
			tdElement.attr("style", "text-align: center; width: 5%;");

			trElement.append(tdElement);

			imgElement = $(document.createElement("img"));
			imgElement.attr("class", "image");
			imgElement
					.attr("src", contextPath + "/resources/images/delete.png");

			aElement = $(document.createElement("a"));
			aElement.append(imgElement);
			aElement.attr("href", "#");
			aElement.attr("alt", labels.entity.remove);
			aElement.attr("title", labels.entity.remove);
			aElement.click({
				id : entity.id
			}, function(event) {
				event.preventDefault();

				if (confirm(labels.entity.removeConfirm)) {
					remove(event.data.id);
				}
			});

			tdElement = $(document.createElement("td"));
			tdElement.append(aElement);
			tdElement.attr("style", "text-align: center; width: 5%;");

			trElement.append(tdElement);

			tbodyElement.append(trElement);
		}

		var paging = data.paging;

		if (paging.hasContent) {
			$(ids.pagination).show();
			$(ids.paginationFrom).text((paging.page + 1));
			$(ids.paginationTo).text((paging.totalPages));

			aElement = $(ids.paginationFirst);

			if (paging.firstPage) {
				aElement.hide();
			} else {
				aElement.show();
				aElement.undelegate();
				aElement.click({
					page : 0
				}, function(event) {
					event.preventDefault();

					pagination.page = event.data.page;

					list();
				});
			}

			aElement = $(ids.paginationPrevious);

			if (paging.hasPreviousPage) {
				aElement.show();
				aElement.undelegate();
				aElement.click({
					page : (paging.page - 1)
				}, function(event) {
					event.preventDefault();

					pagination.page = event.data.page;

					list();
				});
			} else {
				aElement.hide();
			}

			aElement = $(ids.paginationNext);

			if (paging.hasNextPage) {
				aElement.show();
				aElement.undelegate();
				aElement.click({
					page : (paging.page + 1)
				}, function(event) {
					event.preventDefault();

					pagination.page = event.data.page;

					list();
				});
			} else {
				aElement.hide();
			}

			aElement = $(ids.paginationLast);

			if (paging.lastPage) {
				aElement.hide();
			} else {
				aElement.show();
				aElement.undelegate();
				aElement.click({
					page : (paging.totalPages - 1)
				}, function(event) {
					event.preventDefault();

					pagination.page = event.data.page;

					list();
				});
			}
		} else {
			$(ids.pagination).hide();
		}

		$("#loading").hide();
		$(ids.contentList).show();
	};

	success.remove = function(data, textStatus, jqXHR) {
		if (data.success) {
			pagination.page = 0;

			list();
		} else {
			ui.drawErrors(data.errors, ids.errorList);

			$("#loading").hide();
			$(ids.contentList).show();
		}
	};

	success.edit = function(data, textStatus, jqXHR) {
		$(ids.errorEdit).hide();

		$("#form-user-password").val("");

		if (data.entity.id > 0) {
			$("#form-user-id").val(data.entity.id);
			$("#form-user-userName").val(data.entity.userName);
			$("#form-user-password-required").hide();
		} else {
			$("#form-user-id").val("0");
			$("#form-user-userName").val("");
			$("#form-user-password-required").show();
		}

		$("#loading").hide();
		$(ids.contentEdit).show();

		$("#form-user-userName").focus();
	};

	success.save = function(data, textStatus, jqXHR) {
		if (data.success) {
			list();
		} else {
			ui.drawErrors(data.errors, ids.errorEdit);

			$("#loading").hide();
			$(ids.contentEdit).show();
		}
	};

}
